<?php
namespace app\api\controller;

use app\api\controller\Base;
use think\Request;
/**
 * 用户余额页面
 */
class Usermain extends Base
{
     
    public function money()
    {
        
        $user_id =  $this->user_id;
        $page = input('page',1);
        $limit = 10;
        $offset = ($page - 1) * $limit; 
        $type = input('type');
        $where = array();
        if(isset($type)){
            // $type = explode(',',$type);
            $where['ordertype'] = array('in', $type);
        }
        $where['user_id'] = $user_id;
        //用户余额
        $money = db('user')->where(['user_id'=>$user_id])->value('money');
        $second = db('user')->where(['user_id'=>$user_id])->value('second_num');
        //用户消费记录
        $list = db('user_consumption')
        ->where($where)
            ->limit($offset, $limit)
            ->order('addtime desc')
            ->select();
        foreach($list as $k=>&$v){
            $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
        }
        $list = $list?:[];
        return json(['code'=>1,'msg'=>"查询成功",'data'=>['momey'=>$money,'list'=>$list,'second'=>$second]]);
    }
    
    //卡券管理
    public function card()
    {
        $type = input('type',1);
        $page = input('page',1);
        $limit = 10;
        $offset = ($page - 1) * $limit; 
        //优惠券
        if($type==1){
            $list = db('coupon_user')
                ->where(['user_id'=>$this->user_id])
                ->limit($offset, $limit)
                ->order('status asc,id desc')
                ->select();
            foreach($list as $k=>&$v){
                $v['coupon_info'] = unserialize($v['coupon_info']);
                $v['create_time'] = date('Y-m-d',$v['create_time']);
                $v['end_time'] = date('Y-m-d',$v['end_time']);
                $v['coupon_info']['cate'] = db('coupon_cate')->where(['id'=>$v['coupon_info']['cate_id']])->find()['title'];
            }
        }
        
        //次卡
        if($type==2){
            $list = db('second_card_user')
                ->alias('u')
                ->join('second_card c','c.id = u.card_id')
                ->where(['u.user_id'=>$this->user_id])
                ->field('u.id as id,c.num as num,u.card_number as card_number ,u.endtime as endtime,u.card_num as card_num ')
                ->limit($offset, $limit)
                ->order('u.status asc')
                ->select();
            foreach($list as $k=>&$v){ 
                $v['endtime'] = date('Y-m-d',$v['endtime']); 
            }
        }
        
        //充值卡
        if($type==3){
            $list = db('recharge_card')
                ->where(['cate'=>0,'status'=>0])
                ->limit($offset, $limit)
                ->order('addtime desc')
                ->select();
            foreach($list as $k=>&$v){ 
                $v['endtime'] = date('Y-m-d',$v['endtime']); 
            }
        }
        
        $list = $list?:[];
        
        return json(['code'=>1,'msg'=>"查询成功",'data'=>$list]);
        
    }
    
   //我的订单
    public function order()
    {
        $type = input('type', 0);
        $page = input('page', 1);
        $limit = 10;
        $offset = ($page - 1) * $limit;
        $where = " 1 ";
        /**
         * type ==0 全部订单
         * type ==1 待支付     1
         * type ==2 已支付     1
         * type ==3 申请退款   
         * type ==4 退款成功
         * type ==5 退款失败 
         * type ==6 已入住
         * type ==7 已完成
         * 
         * */
        
        switch ($type) {
            case "1":
                $where .= " and a.status=0" ;//待支付
                break;
            case "2":
                $where .= " and a.status=1";//代入驻
                break; 
            case "4":
                $where .= " and a.status=3";//退款成功
                break; 
            case "8":
                $where .= " and a.status=7";//已过期
                break;
            default:
                break;
        }

        $list = db('order')
            ->alias('a')
            ->join('think_room b','a.room_id=b.id','left')
            ->join('think_room_cate c','b.two_cate=c.id','left')
            ->where(['a.user_id'=>$this->user_id,'is_delete'=>0])
            ->where($where)
            ->limit($offset, $limit)
            ->order('a.order_id desc')
            ->field("a.order_id,a.order_number,a.is_comment,type,a.status,a.room_id,a.in_time,a.out_time,a.days,a.money,b.logo,c.title,b.bed,b.description,b.one_cate")
            ->select();
        foreach ($list as $k => &$v) {
            $v['in_time'] = date('Y-m-d', $v['in_time']);
            $v['out_time'] = date('Y-m-d', $v['out_time']);
        }
        $list = $list ?: [];
        return json(['code' => 1, 'msg' => "查询成功", 'data' => $list]);
    }
    
    
    //vip列表
    public function vipcard(){

        //判断用户当前的会员等级
        $user_vip_level = db('user')->where(['user_id'=>$this->user_id])->value('vip_id');
        $user_vipnumber =db('user')->where(['user_id'=>$this->user_id])->value('vip_number');
        //展示用户当前等级下一级的
        $list = db('vip_cate') 
            ->where("level>='{$user_vip_level}' and status=0")
            ->field('id,title,logo,img,cate_id,level,percent,desc')
            ->select();
        foreach($list as $k=>&$v){
            $cate_id = explode(',',$v['cate_id']);
            $v['cate_name'] = db('vip_equity')->where(['id'=>['in',$cate_id]])->field('title,desc,logo')->select();

        }

        $list = $list ?: []; 
        return json(['code' => 1, 'msg' => "查询成功", 'data' => $list,'vip_number'=>$user_vipnumber]);
    }

    //点击立即开通接口
    public function prearbuy(){

        //判断用户当前的会员等级
        $user_vip_level = db('user')->where(['user_id'=>$this->user_id])->value('vip_id');
        //展示用户当前等级下一级的
        $list = db('vip_cate') 
            ->where("level>'{$user_vip_level}' and status=0")
            ->field('id,title,desc,need_money')
            ->select();
        
        $list = $list ?: []; 
        return json(['code' => 1, 'msg' => "查询成功", 'data' => $list]);
    }
    
    
    //评论列表
    public function commentlist(){
        $type = input('type', 0);// 0 带评价 1已评价
        $page = input('page', 1);
        $limit = 15;
        $offset = ($page - 1) * $limit;

        $list = db('order')
            ->alias('a')
            ->join('think_room b','a.room_id=b.id','left')
            ->join('think_room_cate c','b.two_cate=c.id','left')
            ->where(['a.user_id'=>$this->user_id,'a.is_comment'=>$type]) 
            ->where("a.status=1 or a.status=5 or a.status=6")
            ->limit($offset, $limit)
            ->order('a.order_id desc')
            ->field("a.order_id,a.money,b.logo,c.title")
            ->select();

        $list = $list ?: []; 
        return json(['code' => 1, 'msg' => "查询成功", 'data' => $list]);
    }
    
    //评论列表
    public function allcommentlist(){
        $page = input('page', 1);
        $limit = 15;
        $offset = ($page - 1) * $limit;

        $list = db('comment')
            ->alias('c')
            ->join('user u','c.user_id = u.user_id')
            ->where(['c.status'=>1,'u.user_status'=>0])
            ->limit($offset, $limit)
            ->order('id desc')
            ->field('nickname,avatar,addtime,content,images')
            ->select();
        foreach ($list as $i => $item) {
            $list[$i]['addtime'] = date('Y-m-d',$item['addtime']);
            $list[$i]['images'] = explode(',',$item['images']);
        }

        $list = $list ?: []; 
        return json(['code' => 1, 'msg' => "查询成功", 'data' => $list]);
    }
    //立即评论详情
    public function commentinfo(){

        $order_id = input('order_id', 0);
        if(!$order_id) return json(['code' => 0, 'msg' => "参数错误"]);
        
        $list = db('order')
            ->alias('a')
            ->join('think_room b','a.room_id=b.id','left')
            ->join('think_room_cate c','b.two_cate=c.id','left')
            ->where(['a.user_id'=>$this->user_id,'a.order_id'=>$order_id]) 
            ->field("a.order_id,a.money,b.logo,c.title,b.bed,b.description")
            ->find();
        if(empty($list)) return json(['code' => 0, 'msg' => "订单不存在"]);

        return json(['code' => 1, 'msg' => "查询成功", 'data' => $list]);
    }

    //立即评论
    public function comment(){
         
        $data = [
            'user_id' => $this->user_id,
            'order_id' => input('order_id'),
            'content' => input('content'),
            'images' => input('images'),
            'addtime' => time(), 
        ];

        db('comment')->insert($data);
        db('order')->where(['order_id'=>$data['order_id']])->update(['is_comment'=>1]);
        
        return json(['code' => 1, 'msg' => "评论成功"]);
    }
    
    //已经评论的评论详情 
    public function usecommentinfo(){

        $order_id = input('order_id', 0);
        if(!$order_id) return json(['code' => 0, 'msg' => "参数错误"]);
        
        $room = db('order')
            ->alias('a')
            ->join('think_room b','a.room_id=b.id','left')
            ->join('think_room_cate c','b.two_cate=c.id','left')
            ->where(['a.user_id'=>$this->user_id,'a.order_id'=>$order_id]) 
            ->field("a.order_id,a.money,b.logo,c.title,b.bed,b.description")
            ->find();
        
        if(empty($room)) return json(['code' => 0, 'msg' => "订单不存在"]);
        
        //评论
        $comment = db('comment')->where(['order_id'=>$order_id])->find();
        $comment['addtime'] = date('Y年m月d日 H:i:s',$comment['addtime']);
        $comment['images'] = explode(',',$comment['images']);
        
        return json(['code' => 1, 'msg' => "查询成功", 'data' => ['list'=>$list,'comment'=>$comment]]);
    }
    public function paypwd(){
        /*
        0 已经购买了过 但是没有设置
        3 没有购买过也没有设置过
        */
        $user_id = $this->user_id;
        $user = db('user')->where(['user_id'=>$user_id])->find();
        if(isset($user['paypwd']) && !$user['paypwd']!=''){
             return ['code'=>1,'data'=>1];
            
        }else{
            if(isset($user['money']) && !$user['money']*1!=0 || isset($user['second_num']) && !$user['second_num']*1!=0){
               return ['code'=>1,'data'=>0];
            }else{
                return ['code'=>1,'data'=>3];
            }
        }
    }

   
    
}
